<?php
namespace app\erp\controller;

use app\erp\model\Departmentdb;
use app\erp\model\Sitedb;

class Department extends Base
{

    protected $cols = array(
        array(
            'field' => 'department_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'site_id_text',
            'width' => 160,
            'title' => '所属分站',
            'sort' => 'true'
        ),
        array(
            'field' => 'department_name',
            'width' => 160,
            'title' => '部门名称',
            'sort' => 'true'
        ),
        array(
            'field' => 'create_time',
            'width' => 120,
            'title' => '录入时间',
            'sort' => 'true'
        )
    );

    public function index()
    {
        array_push($this->cols, array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#department_tool'
        ));
        $this->assign('cols', json_encode($this->cols));
        // 调用分站数据
        $sitedb = new Sitedb();
        $sitedb->param = array(
            '_o' => 'site_id',
            '_b' => 'desc',
            'page' => 1,
            'limit' => 30
        );
        $get_site_list = $sitedb->ajaxSelect();
        $this->assign('get_site_list', $get_site_list['data']);
        return $this->fetch();
    }

    public function ajaxDepartment()
    {
        $request = request();
        $param = $request->param();
        $departmentdb = new Departmentdb();
        $departmentdb->param = $param;
        $return = $departmentdb->ajaxSelect();
        $this->layuiReturn($return['data'], $return['count']);
    }
    /**
     *
     * @todo 获取单条数据
     */
    public function ajaxOneData()
    {
        $request = request();
        $param = $request->param();
        $departmentdb = new Departmentdb();
        $data = $departmentdb->ajaxFind($param['department_id']);
        $this->layuiReturn($data, 1);
    }

    public function tplForm()
    {
        $request = request();
        $param = $request->param();
        $data = array();
        $department_id = '';
        if (isset($param['department_id']) && ! empty($param['department_id'])) {
            // 获取数据
            $departmentdb = new Departmentdb();
            $data = $departmentdb->ajaxFind($param['department_id']);
            $department_id = $param['department_id'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('department_id', $department_id);
        return $this->fetch();
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $request = request();
        $param = $request->param();
        $departmentdb = new Departmentdb();
        $return = $departmentdb->ajaxSave($param);
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, - 1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }
    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $request = request();
        $param = $request->param();
        $departmentdb = new Departmentdb();
        $departmentdb->param = $param;
        $departmentdb->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }
    public function ajaxRestore(){
        $request = request();
        $param = $request->param();
        $departmentdb = new Departmentdb();
        $departmentdb->param = $param;
        $departmentdb->ajaxRestore();
        $this->layuiReturn('数据恢复成功',0);
    }
    // 回收站
    public function recycle()
    {
        array_push($this->cols, array(
            'field' => 'delete_time',
            'width' => 120,
            'title' => '删除时间'
        ),array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#recycle_department_tool'
        ));
        $this->assign('cols', json_encode($this->cols));
        // 调用分站数据
        $sitedb = new Sitedb();
        $sitedb->param = array(
            '_o' => 'site_id',
            '_b' => 'desc',
            'page' => 1,
            'limit' => 30
        );
        $get_site_list = $sitedb->ajaxSelect();
        $this->assign('get_site_list', $get_site_list['data']);
        return $this->fetch();
    }
}